<script setup>

 function promiseState(){
    const p1 = new Promise(function(resolve,reject){
        resolve('success1');
        resolve('success2');
    }); 
    const p2 = new Promise(function(resolve,reject){  
        resolve('success3'); 
        reject('reject');
    });

    p1.then(function(value){  
        console.log(value); // success1
    });
    p2.then(function(value){ 
        console.log(value); // success3
    });
 }


var promiseBase=function(){
    const promise = new Promise((resolve, reject) => {
        // 模拟异步操作
        setTimeout(() => {
            const randomNumber = Math.random();
            if (randomNumber > 0.5) {
                resolve(randomNumber);
            } else {
                reject(new Error('随机数小于 0.5'));
            }
        }, 1000);
    });

    promise.then((result)=>{
        console.log('异步操作成功：',result); // 随机数大于 0.5
    },(error)=>{
        console.log('异步操作失败：',error);
    });

    promise.catch((error) => {
        console.log('catch异步操作失败：', error);
    });

    /*
    promise.finally(() => {
        console.log('异步操作完成');
    });
    */
};

var promiseBase2=function(){
    const promise = new Promise((resolve, reject) => {
        // 模拟异步操作
        setTimeout(() => {
            const randomNumber = Math.random();
            if (randomNumber > 0.5) {
                resolve(randomNumber);
            } else {
                reject(new Error('随机数小于 0.5'));
            }
        }, 1000);
    });

    promise.then((result)=>{
        console.log('异步操作成功：',result); // 随机数大于 0.5
    }).catch((error) => {
        console.log('catch异步操作失败：', error);
    }).finally(() => {
        console.log('异步操作完成');
    });
};

function promiseThen(){
    const p = new Promise(function(resolve,reject){
        resolve('success');
    });
    
    p.then(function(value){
        console.log(value);
    });
    
    console.log('first');
}

function promiseMoreThen(){
    const p = new Promise(function(resolve,reject){
        resolve(1);
    }).then(function(value){ // 第一个then // 1
        console.log(value);
        return value * 2;
    }).then(function(value){ // 第二个then // 2
        console.log(value);
    }).then(function(value){ // 第三个then // undefined
        console.log(value);
        return Promise.resolve('resolve'); 
    }).then(function(value){ // 第四个then // resolve
        console.log(value);
        return Promise.reject('reject'); 
    }).then(function(value){ // 第五个then //reject:reject
        console.log('resolve:' + value);
        }, function(err) {
        console.log('reject:' + err);
    });
}

function promiseChain(){
    const promise1 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('第一个异步操作完成');
        }, 500);
   });

   const promise2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('第二个异步操作完成');
        }, 1500);
   });

   promise1.then((result) => {
       console.log(result);
       return promise2;
    })
    .then((result) => {
        console.log(result);
    })
    .catch((error) => {
        console.log('异步操作失败：', error);
    })
    .finally(() => {
        console.log('所有异步操作完成');
    });
}

function promiseAll(){
    const promise1 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('第一个异步操作完成');
        }, 500);
   });

   const promise2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('第二个异步操作完成');
        }, 1500);
   });

   const promise3 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('第三个异步操作完成');
        }, 1200);
    });

    const promise4 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('第四个异步操作完成');
        }, 1800);
    });

    Promise.all([promise1, promise2, promise3, promise4])
        .then((results) => {
            console.log(results);
        })
        .catch((error) => {
            console.log('异步操作失败：', error);
        });
}

function promiseSequence(){
    const step1 = () => {
        return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('第一步开始');
            resolve('第一步完成');
        }, 1000);
        });
    };

    const step2 = (result1) => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('第二步开始');
                resolve(result1 + ', 第二步完成');
            }, 1500);
        });
    };

    const step3 = (result2) => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('第三步开始');
                resolve(result2 + ', 第三步完成');
            }, 1200);
        });
    };

    step1()
        .then(step2)
        .then(step3)
        .then((result3) => {
            console.log(result3);
        })
        .catch((error) => {
            console.log('异步操作失败：', error);
        });
}
</script>
<template>
    <button @click="promiseSequence">按钮</button>
</template>